package org.chl.chapter06;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/book")
public class BookController {

	@Autowired
	BookDao bookDao;
	
	@Autowired
    MongoTemplate mongoTemplate;

	@PostMapping("addBook")
	public Book addBook(@RequestBody Book book) {
		return bookDao.insert(book);
	}

	@PostMapping("updateBook")
	public Book updateBook(@RequestBody Book book) {
		mongoTemplate.save(book);
		return book;
	}

	@GetMapping("deleteBookById")
	public void deleteBookById(Integer id) {
		bookDao.deleteById(id);
	}

	@GetMapping("getBookById")
	public Book getBookById(Integer id) {
		return mongoTemplate.findById(id, Book.class);
	}

	@GetMapping("getAllBooks")
	public List<Book> getAllBooks() {
		return bookDao.findAll();
	}
}
